/* GUI CONVERTED from reoptre1.p (converter v1.75) Wed Aug 16 23:18:41 2000 */
/* reoptre1.p - REPETITIVE SCRAP TRANSACTION UPDATE SCHEDULE            */
/* Copyright 1986-2002 QAD Inc., Carpinteria, CA, USA.                  */
/* All rights reserved worldwide.  This is an unpublished work.         */
/*F0PN*/ /*V8:ConvertMode=Maintenance                                   */
/*K1Q4*/ /*V8:RunMode=Character,Windows                                 */
/* REVISION: 7.3      LAST MODIFIED: 02/23/93   BY: emb *G760*          */
/* REVISION: 7.3      LAST MODIFIED: 12/16/93   BY: emb *GI15*          */
/* REVISION: 7.3      LAST MODIFIED: 07/20/94   BY: pxd *GK84*          */
/* REVISION: 7.3      LAST MODIFIED: 08/10/94   BY: ais *GL19*          */
/* Oracle changes (share-locks)      09/12/94   BY: rwl *FR25*          */
/* REVISION: 7.3      LAST MODIFIED: 02/15/95   BY: ais *G0FC*          */

/* REVISION: 8.6E     LAST MODIFIED: 02/23/98   BY: *L007* A. Rahane    */
/* REVISION: 8.6E     LAST MODIFIED: 05/20/98   BY: *K1Q4* Alfred Tan   */
/* REVISION: 8.6E     LAST MODIFIED: 06/08/98   BY: *H0YY* Niranjan R.  */
/* REVISION: 9.1      LAST MODIFIED: 08/08/00   BY: *N0GD* Ganga Subramanian*/

/* ********** Begin Translatable Strings Definitions ********* */

/*N0GD* &SCOPED-DEFINE reoptre1_p_1 "Scrap Requirement" */
/*N0GD* /* MaxLen: Comment: */ */

/* ********** End Translatable Strings Definitions ********* */

     {mfdeclre.i}

/*N0GD*/ {gplabel.i}

     define shared variable ophist_recid as recid.
     define shared variable schedule_id like wo_lot.

     define variable wr_recid as recid.
     define variable qty_left like op_qty_comp.
     define variable i as integer.
     define variable temp_qty like op_qty_comp no-undo.
     define variable temp_lot like wo_lot no-undo.
     define variable qty_open like mrp_qty no-undo.
     define variable ii as integer no-undo.
     define variable cons_bck like rpc_sch_bck no-undo.
     define variable cons_fwd like rpc_sch_fwd no-undo.
     define variable rpc_start like rpc_wk_start no-undo.
     define variable start_date like wo_due_date no-undo.
     define variable min_date like wo_due_date no-undo.
     define variable last-op like mfc_logical initial yes.
     define variable rpsnbr like mrp_nbr.
     define variable open_qty like mrp_qty.

/*N0GD*/ define variable msg1 as character format "x(22)" no-undo.

/*N0GD*/ msg1 = getTermLabel("SCRAP_REQUIREMENT",22).

     find op_hist where recid(op_hist) = ophist_recid no-lock no-error.
     if not available op_hist then leave.

     qty_left = op_qty_comp.

     find first rpc_ctrl no-lock no-error.
     if available rpc_ctrl then
     assign cons_bck = rpc_sch_bck
        cons_fwd = rpc_sch_fwd
/*G0FC*/              ii = - rpc_sch_bck - 1
           rpc_start = rpc_wk_start.

     if schedule_id > "" then do:
        find wo_mstr no-lock where wo_lot = schedule_id no-error.
        start_date = wo_due_date.
     end.

     if start_date = ? then start_date = op_date.

     if rpc_start = 8 then min_date = max(today,start_date - cons_bck).
     else
     if rpc_start = 0 then min_date = start_date - cons_bck.
     else do:
        if weekday(start_date) >= rpc_start
        then min_date = max(start_date - cons_bck,
           start_date - weekday(start_date) + rpc_start).
        else min_date = max(start_date - cons_bck,
           start_date - weekday(start_date) + rpc_start - 7).
     end.

     temp_lot = schedule_id.
     do while qty_left <> 0:

        find wr_route no-lock where wr_lot = temp_lot
        and wr_op = op_wo_op no-error.

        do while qty_left <> 0:

           if available wr_route
           and ((qty_left > 0 and wr_qty_ord > wr_qty_comp)
        or qty_left < 0)
           then do:

          if qty_left > 0 then do:
             temp_qty = max(wr_qty_ord
                  - max(wr_qty_comp,0),0).
             temp_qty = min(temp_qty,qty_left).
             qty_left = max(qty_left - temp_qty,0).
          end.
          else do:
             temp_qty = qty_left.
             qty_left = 0.
          end.

          /* UPDATE REPETITIVE SCHEDULE WORK ORDER */
          wr_recid = recid(wr_route).
          find wr_route where recid(wr_route) = wr_recid.
          wr_qty_rjct = wr_qty_rjct - temp_qty.
/*GL19*/          wr_qty_ord = wr_qty_ord - temp_qty.

          if last-op then do:
             find wo_mstr exclusive-lock where wo_lot = wr_lot no-error.
/*H0YY*/         find in_mstr exclusive-lock
/*H0YY*/          where in_part = wo_part and in_site = wo_site no-error.
/*H0YY*/     if available in_mstr then do:
/*H0YY*/        if wo_qty_ord >= 0 then
/*H0YY*/           in_qty_ord = in_qty_ord - max(wo_qty_ord - wo_qty_comp
/*H0YY*/                              - wo_qty_rjct,0).
/*H0YY*/        else
/*H0YY*/           in_qty_ord = in_qty_ord - min(wo_qty_ord - wo_qty_comp
/*H0YY*/                               - wo_qty_rjct,0).
/*H0YY*/     end.

             wo_qty_rjct = wo_qty_rjct + temp_qty.

/*H0YY*/         if available in_mstr then do:
/*H0YY*/        if wo_qty_ord >= 0 then
/*H0YY*/           in_qty_ord = in_qty_ord + max(wo_qty_ord - wo_qty_comp
/*H0YY*/                              - wo_qty_rjct,0).
/*H0YY*/            else
/*H0YY*/           in_qty_ord = in_qty_ord + min(wo_qty_ord - wo_qty_comp
/*H0YY*/                                  - wo_qty_rjct,0).
/*H0YY*/         end.

             open_qty = max(wo_qty_ord
                  - max(wo_qty_comp,0)
                  - max(wo_qty_rjct,0),0).

             {mfmrw.i "wo_mstr" wo_part wo_nbr wo_lot """"
              wo_rel_date wo_due_date
              open_qty "SUPPLYF" " " wo_site}
/*N0GD* *****************BEGIN DELETED CODE **********************
/*GK84*/      {mfmrw.i "wo_scrap" wo_part wo_nbr wo_lot """"
              wo_rel_date wo_due_date
              "open_qty * (1 - wo_yield_pct / 100)"
              "DEMAND"
              {&reoptre1_p_1}
               wo_site}
 *N0GD* ***************** END DELETED CODE ************************* */

/*N0GD*/     {mfmrw.i "wo_scrap" wo_part wo_nbr wo_lot """"
              wo_rel_date wo_due_date
              "open_qty * (1 - wo_yield_pct / 100)"
              "DEMAND"
               msg1
               wo_site}

             /* UPDATE REPETITIVE SCHEDULE RECORD */
             find rps_mstr exclusive-lock where rps_part = wo_part
             and rps_due_date = wo_due_date
             and rps_site = wo_site
             and rps_line = wo_line no-error.
             rps_qty_comp = rps_qty_comp + temp_qty.
          end.

          /* Update later operations to reduce quantity expected */
/*FR25*/  for each wr_route exclusive-lock where wr_lot = schedule_id
          and wr_op > op_wo_op
          by wr_lot by wr_op:
/*GUI*/ if global-beam-me-up then undo, leave.


             /* Reduce component requirements given scrapped quantity */
/*FR25*/     for each wod_det exclusive-lock where wod_lot = wr_lot
             and wod_nbr = wr_nbr
             and wod_op = wr_op
             and wod_qty_req <> 0:
/*GUI*/ if global-beam-me-up then undo, leave.


/*H0YY*/         find in_mstr exclusive-lock
/*H0YY*/          where in_part = wod_part and in_site =
/*H0YY*/          wod_site no-error.
/*H0YY*/     if available in_mstr then do:
/*H0YY*/        if wod_qty_req >= 0 then
/*H0YY*/           in_qty_req = in_qty_req -
/*H0YY*/                    max(wod_qty_req - wod_qty_iss,0).
/*H0YY*/        else
/*H0YY*/           in_qty_req = in_qty_req -
/*H0YY*/                min(wod_qty_req - wod_qty_iss,0).
/*H0YY*/     end.

                 wod_qty_req =
                        max(min(wr_qty_ord - temp_qty,wo_qty_ord),0)
                        * wod_qty_req / wr_qty_ord.

/*H0YY*/         if available in_mstr then do:
/*H0YY*/        if wod_qty_req >= 0 then
/*H0YY*/               in_qty_req = in_qty_req +
/*H0YY*/                        max(wod_qty_req - wod_qty_iss,0).
/*H0YY*/        else
/*H0YY*/               in_qty_req = in_qty_req +
/*H0YY*/                        min(wod_qty_req - wod_qty_iss,0).
/*H0YY*/     end.

               {mfmrwdel.i "wod_det" wod_part wod_nbr wod_lot """"}

               if wod_qty_req >= 0
               then qty_open = max(wod_qty_req - max(wod_qty_iss,0),0).
               else qty_open = min(wod_qty_req - min(wod_qty_iss,0),0).

               {mfmrw.i "wod_det" wod_part wod_nbr wod_lot
            string(wod_op) ? wod_iss_date
            qty_open "DEMAND" " " wod_site}
             end.
/*GUI*/ if global-beam-me-up then undo, leave.


             wr_qty_ord = max(min(wr_qty_ord - temp_qty,wo_qty_ord),0).
          end.
/*GUI*/ if global-beam-me-up then undo, leave.

           end.

/*GI15*/       release wr_route.

           /* Find other repetitive schedule orders to offset */
           if qty_left <> 0 then do:

          next-order:
          repeat:

             if temp_lot > "" then do:
/*G0FC*                 if ii <= 0 then do:
      *                    ii = ii - 1.
      *                    if ii < - cons_bck then ii = 0.
      *                    if start_date + ii < min_date then ii = 0.
      *                 end.
      *                 if ii >= 0 then do:
**G0FC*/
            ii = ii + 1.
            if ii > cons_fwd then leave.
/*G0FC                  end.  */
             end.
             temp_lot ="X". /* So it's not empty 2nd time thru */

             find rps_mstr exclusive-lock where rps_part = op_part
             and rps_site = op_site
             and rps_line = op_line
             and rps_due_date = start_date + ii
             and ((qty_left > 0 and rps_qty_req > rps_qty_comp)
              or qty_left < 0) no-error.
             if not available rps_mstr then next.
             temp_lot = string(rps_record).

             find wr_route no-lock where wr_lot = temp_lot
             and wr_op = op_wo_op no-error.

             if available wr_route then leave next-order.

          end.
           end.
           if not available wr_route then leave.
        end.
        if not available wr_route then leave.
     end.
